package com.leyou.trade.config;

import com.leyou.auth.dto.UserDetails;
import com.leyou.auth.utils.UserContext;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

//分表，动态计算集合名
@Component
public class CollectionNameBuilder {
    @Value("${ly.mongo.collectionNamePrefix}")
    private String namePrefix;//集合名（表名）前缀

    public String build() {
        //获取当前登录用户
        UserDetails user = UserContext.getUser();
        if (user == null) {
            return "";
        }
        //用一个固定collection名前缀，拼接上userId对100取余得到的数字，作为collection名
        return namePrefix + user.getId().hashCode() % 100;
    }
}
